﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mail;
using System.Xml;
using System.Text.RegularExpressions;

namespace TestOnlineProject.Common
{
    public class StringHelper
    {
        public int[] Split(string ids)
        {
            string[] idStr = ids.Split('-');
            int[] idInt = new int[idStr.Length - 1];

            // Convert string array to int array.
            for (int i = 0; i < idStr.Length - 1; i++)
            {
                try
                {
                    idInt[i] = int.Parse(idStr[i]);
                }
                catch (Exception)
                {
                    //
                }
            }

            return idInt;
        }

        /// <summary>
        /// Create new String
        /// </summary>
        /// <param name="size">Number of character in string</param>
        /// <returns>New String</returns>

        public string RandomString(int size)
        {
            try
            {
                var random = new Random((int) DateTime.Now.Ticks);
                var builder = new StringBuilder();
                for (var i = 0; i < size; i++)
                {
                    var nextDouble = random.NextDouble();
                    char ch;
                    if (nextDouble <= 0.5)
                    {
                        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(18 * nextDouble + 48)));
                    }
                    else
                    {
                        ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                    }
                    builder.Append(ch);
                }
                return builder.ToString();
            }
            catch (Exception)
            {
                return "";
            }

        }

        /// <summary>
        /// Send Mail
        /// </summary>
        /// <param name="email">E-mail to send</param>
        /// <param name="newPassword">String new password</param>
        /// <returns></returns>

        public bool SendMail(string email, string newPassword)
        {
            try
            {
                var mail = new MailMessage
                {
                    To = email,
                    From = "testonlineproject@gmail.com",
                    Subject = "Reset Password",
                    Body = newPassword
                };

                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "2");
                //set your username here
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "testonlineproject@gmail.com");
                //set your password here
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "testonlineproject123");
                //set your port here
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "465");
                //set your password here
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", "smtp.gmail.com");
                //set SSL option to true
                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", true);

                SmtpMail.Send(mail);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static string HtmlToPlainText(string html)
        {
            if (html == null) {
                return ""; 
            }
            return Regex.Replace(html, @"<(.|\n)*?>", "").Replace("&nbsp;","").Replace("\r","").Replace("\n","").Trim();
        }
    }
}